﻿$(function(){ 
	$.fn.uqcmsupload = function(options){
		var defaults = {
			'module':'0' 
		};
		//var settings = $.extend({}, defaults, options);
		$this = this;
		$(document).on('change', $this, function(e){
			if($('.upprocess').length && $('.upprocess').length > 0){
				
			}else{
				html = '<div class="upprocess"></div>';
				$this.parent().after(html); 
				var X = $this.parent().offset().top; 
				var Y = $this.parent().offset().left;  
				$(".upprocess").css({left:Y,top:X+30}); 
				
				var files  = e.currentTarget.files; 
				for(var i=0; i<files.length; i++){
					if(!files[i]['name'].match(/.jpg|.gif|.png|.bmp/i)){　　//判断上传文件格式
						return alert("上传的图片格式不正确，请重新选择");
					}else{ 
						var timestamp = Date.parse(new Date());
						timestamp = timestamp / 1000;
						ii = i+timestamp;
						pro_html = '<div class="upprcs_one upprcs_one_'+ii+'"><em><img src="<{$__PUBLIC__}>images/simg.png"/></em><em>'+files[i]['name']+'</em><em>'+files[i]['size']+'k</em><em>上传中</em></div>';
						if($('.upprocess .upprcs_one').length && $('.upprocess .upprcs_one').length > 0){
							$(".upprocess .upprcs_one:first-child").before(pro_html);
						}else{ 
							$(".upprocess").html(pro_html);
						} 
						// uploadAction(files[i], '.upprcs_one_'+ii); 
					}
				}/*  */ 
			} 
		});
		function uploadAction(file, upprcs_id){ 
			var fd = new FormData();
			fd.append("myfile", file);
			////upprcs_id = ".upprcs_one_"+i;
			$.ajax({
				url: "member.php?m=album&a=test_up&path=goods",
				type: "POST",
				data: fd,
				processData: false, // 告诉jQuery不要去处理发送的数据
				contentType: false, // 告诉jQuery不要去设置Content-Type请求头
				xhr: function(){ 
					xhr = $.ajaxSettings.xhr();	
					if(xhr.upload) {
						xhr.upload.addEventListener("progress" ,(function(evt){  
							loaded = evt.loaded;     			//已经上传大小情况 
							tot = evt.total;      				//附件总大小 
							per = Math.floor(100*loaded/tot);  	//已经上传的百分比  
							//$(upprcs_id+" em:last").css("width", per+"%");		//进度条，没有就不弄了
							$(upprcs_id+" em:last").html(per+"%");
							if(per == 100){
								$(upprcs_id+" em:last").html('<span style="color:red;">√</span>');
								$(upprcs_id).attr('data-ok','1');
								upload_check();
							}
						}), false);
						return xhr;
					}
				} 
			}); 
		}
		
		function upload_check(){
			var total = 0;
			var ok = 0;
			$(".upprcs_one").each(function(){
				$this = $(this);
				if($this.attr('data-ok') == '1'){
					ok++;
				}
				total++;
			}); 
			if(total == ok){
				$(".upprocess").html('').hide();
				alert('上传完成');
				location.reload(); 
			}else{
				return false;
			}
		}
	}  	   
});
 